package com.wine.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wine.common.core.domain.AjaxResult;
import com.wine.common.utils.StringUtils;
import com.wine.exam.core.constants.ExamState;
import com.wine.exam.domain.PaperAnswer;
import com.wine.exam.mapper.PaperAnswerMapper;
import com.wine.exam.service.IPaperAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 【试卷答案表】Service业务层处理
 *
 * @author wine
 * @date 2022-12-15
 */
@Service
public class PaperAnswerServiceImpl extends ServiceImpl<PaperAnswerMapper, PaperAnswer> implements IPaperAnswerService {
    @Autowired
    PaperAnswerMapper paperAnswerMapper;

    /**
     * 查询是否可以考试
     *
     * @param paperAnswer
     * @return
     */
    @Override
    public AjaxResult preserve(PaperAnswer paperAnswer) {


        //       存在时 提交创建时，初始化基本数据

        LambdaQueryWrapper<PaperAnswer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaperAnswer::getPaperId, paperAnswer.getPaperId());
        wrapper.eq(PaperAnswer::getUserId, paperAnswer.getUserId());
//        这里要想下
        wrapper.notBetween(PaperAnswer::getDoExamining, ExamState.DO_EXAM, ExamState.OVER_EXAM);
        boolean exists = paperAnswerMapper.exists(wrapper);
        System.out.println("exists" + exists);
        if (exists) {
            return AjaxResult.error("试卷不存在或者已考该试卷");
        } else {

            int insert = paperAnswerMapper.insert(paperAnswer);
            System.out.println("paperAnswer" + paperAnswer);
            HashMap<String, Object> map = new HashMap<>();
            map.put("doExam", true);
            /*前端不需要開始時間，只需要結束時間就行了*/
//            map.put("doExamStartTime", paperAnswer.getExamStartTime());
            map.put("doExamEndTime", paperAnswer.getExamEndTime());
            map.put("paperAnswerId", paperAnswer.getPaperAnswerId());
            return AjaxResult.success(map);
        }
    }
}
